<!-- 定标结果确认 -->
<template>
	<view>
		<u-form :model="model" :rules="rules" ref="form" labelPosition="top" labelWidth="auto" :labelStyle="{
			fontSize: '14px',
			color: '#B2B2B2',
			lineHight: '20px'
		}">
			<view class="basic-style">
				<view class="basic-title basic-border">基本信息</view>
				<u-form-item label="单据编号" prop="billNo" required>
					<u--input v-model="model.billNo" disabled></u--input>
				</u-form-item>
				<u-form-item label="申请人" prop="proposer" required>
					<h-select-person-input style="width: 100%" disabled v-model="model.proposer" placeholder="请选择申请人"
						border="surround" suffixIcon="arrow-down-fill" :suffixIconStyle="{ color: '#B2B2B2' }"
						objectField="model.proposer"></h-select-person-input>
				</u-form-item>
				<u-form-item label="申请部门" prop="proposerDeptId" required>
					<h-select-depart-input v-model="model.proposerDeptId" :departName="model.proposerDeptText"
						placeholder="请选择部门" border="surround" disabled style="width: 100%" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						:title.sync="model.proposerDeptText"></h-select-depart-input>
				</u-form-item>
				<u-form-item label="申请时间" prop="createTime" required>
					<u--input v-model="model.createTime" disabled suffixIcon="calendar-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }" placeholder="请选择日期"></u--input>
				</u-form-item>
				<u-form-item label="申请金额(元)" prop="applyMoney" required>
					<h-money-input placeholder="请输入申请金额" v-model="model.applyMoney"></h-money-input>
				</u-form-item>
				<u-form-item label="调拨分类" prop="allotType" required>
					<h-select-input-tag style="width: 100%" dictName="allot_type" v-model="model.allotType"
						placeholder="请选择调拨分类" border="surround" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"></h-select-input-tag>
				</u-form-item>
				<u-form-item label="使用日期" prop="usedDate" required>
					<h-time-select mode="date" ref="usedDate" v-model="model.usedDate" placeholder="请选择使用日期"
						border="surround" suffixIcon="calendar-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"></h-time-select>
				</u-form-item>
				<u-form-item label="预计归还日期" prop="anticipateReturnDate" required>
					<h-time-select mode="date" ref="anticipateReturnDate" v-model="model.anticipateReturnDate"
						placeholder="请选择预计归还日期" border="surround" suffixIcon="calendar-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"></h-time-select>
				</u-form-item>
				<u-form-item label="资金用途" prop="purpose" required>
					<u--textarea :confirm-type="null" maxlength="100" count placeholder="请输入资金用途"
						v-model="model.purpose"></u--textarea>
				</u-form-item>
				<u-form-item label="调出公司" prop="allotOutUnit" required>
					<!-- <h-select-company-input-popup style="width: 100%" v-model="model.allotOutUnit" commercialType="1"
						placeholder="请选择调出公司" border="surround" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"></h-select-company-input-popup> -->
						
						<h-select-company-input
						  style="width: 100%"
						  v-model="model.allotOutUnit"
						  border="surround"
						  placeholder="请选择调出公司"
							commercialType="1"
						  suffixIcon="arrow-down-fill"
						  :suffixIconStyle="{ color: '#B2B2B2' }"
						  objectField="model.allotOutUnit"
						  @input="changeCompany"
						></h-select-company-input>
				</u-form-item>
				<u-form-item label="调出团队" prop="allotOutTeam" required>
					<h-select-team-input style="width: 100%" v-model="model.allotOutTeam" objectField="model.allotOutTeam"
						placeholder="请选择调出团队" border="surround" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }" :tdlx="3" />
				</u-form-item>
				<u-form-item label="调入公司" prop="allotInUnit" required>
					<!-- <h-select-company-input-popup style="width: 100%" v-model="model.allotInUnit" commercialType="1"
						placeholder="请选择调入公司" border="surround" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"></h-select-company-input-popup> -->
						
						<h-select-company-input
						  style="width: 100%"
						  v-model="model.allotInUnit"
						  border="surround"
						  placeholder="请选择调入公司"
							commercialType="1"
						  suffixIcon="arrow-down-fill"
						  :suffixIconStyle="{ color: '#B2B2B2' }"
						  objectField="model.allotInUnit"
						  @input="changeCompany"
						></h-select-company-input>
				</u-form-item>
				<u-form-item label="调入团队" prop="allotInTeam" required>
					<h-select-team-input style="width: 100%" v-model="model.allotInTeam" objectField="model.allotInTeam"
						placeholder="请选择调入团队" border="surround" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }" :tdlx="3" />
				</u-form-item>
			</view>

			<view class="basic-style">
				<view class="basic-title basic-border">附件信息</view>
				<view class="py-1 space-y-1" style="width: 100%">
					<view style="color: red">提示：请等待附件上传完成，再点击保存，否则附件无法保存成功！</view>
					<h-upload-file v-model="model.attachment" :isIcon="false"></h-upload-file>
					<file-list v-if="model.attachment" v-model="model.attachment" :isDelete="true"></file-list>
				</view>
			</view>

			<view class="space-x-1 px-1 btn_position">
				<u-button :disabled="isSubmit" type="primary" :plain="true" text="保存" @click="onDraft"></u-button>
				<u-button :disabled="isSubmit" type="primary" text="保存并提交" @click="onSubmit"></u-button>
			</view>
			<view style="height: 100rpx;"></view>
		</u-form>
		<u-toast ref="uToast"></u-toast>
		<ProcessNodeConfiguration ref="processConfig" @ok="onOk"></ProcessNodeConfiguration>
	</view>
</template>
  
<script>
import { mapState } from 'vuex'
import moment from 'moment'
import { gainBillNo, add, info, edit } from '@/api/finance/ErpCapitalAllotForm'

import fileList from '@/pages/synergy/components/fileList.vue'
import ProcessNodeConfiguration from '@/components/ProcessNodeConfiguration/ProcessNodeConfiguration.vue' // 流程节点配置人员选择弹窗
export default {
	name: 'ErpPurchaseApplyForm',
	components: {
		fileList,
		ProcessNodeConfiguration
	},
	data() {
		return {
			isSubmit: false, // 控制按钮是否可以点击
			model: {
				// 基本信息 - 公用
				bpmBizTitle: '',
				billNo: '',
				proposer: '',
				proposerDeptId: '',
				createTime: '',
				applyMoney: '',
				allotType: "",
				usedDate: "",
				anticipateReturnDate: "",
				purpose: "",
				allotOutUnit: "",
				allotOutTeam: "",
				allotInUnit: "",
				allotInTeam: "",
				// 附件
				attachment: null,
			},
			provisionBroadups: null,
			rules: {
				billNo: [{ required: true, message: '请输入单据编号!', trigger: ['blur', 'change'] }],
				proposerDeptId: [{ required: true, message: '请选择申请部门!', trigger: ['blur', 'change'] }],
				proposer: [{ required: true, message: '请选择申请人!', trigger: ['blur', 'change'] }],
				createTime: [{ required: true, message: '请选择申请时间!', trigger: ['blur', 'change'] }],
				applyMoney: [
					{ required: true, type: 'number', message: '请输入申请金额!', trigger: ['blur', 'change'] },
					{ required: true, pattern: /^(?!0\.00)(?!0$)([1-9]\d*|0)(\.\d{2})?$/, message: '请输入正确的金额!', trigger: ['blur', 'change'] },
				],
				allotType: [{ required: true, message: '请选择调拨分类!', trigger: ['blur', 'change'] }],
				usedDate: [{ required: true, message: '请选择使用日期!', trigger: ['blur', 'change'] }],
				anticipateReturnDate: [{ required: true, message: '请选择预计归还日期!', trigger: ['blur', 'change'] }],
				purpose: [{ required: true, message: '请输入资金用途!', trigger: ['blur', 'change'] }],
				allotOutUnit: [{ required: true, message: '请选择调出公司!', trigger: ['blur', 'change'] }],
				allotOutTeam: [{ required: true, message: '请选择调出团队!', trigger: ['blur', 'change'] }],
				allotInUnit: [{ required: true, message: '请选择调入公司!', trigger: ['blur', 'change'] }],
				allotInTeam: [{ required: true, message: '请选择调入团队!', trigger: ['blur', 'change'] }],
			},

			//代码生成后需手动设置流程编码
			flowCode: 'onl_erp_capital_allot',
			formUrl: "erp/finance/capitalAllot/modules/ErpCapitalAllotForm",
			beanName: 'erpCapitalAllotServiceImpl',
			formUrlMobile: "/pages/synergy/erpCapitalAllotList",
			processId: "",
		}
	},
	onLoad({ id }) {
		this.id = id || ''
	},
	created() {
		if (this.id) {
			this.initInfo()
		} else {
			this.initForm()
			this.initBillNo()
		}
		// this.initBuyerInfo()
	},
	computed: {
		...mapState(['userInfo']),
	},
	methods: {
		// 获取单据编号
		async initBillNo() {
			const res = await gainBillNo()
			this.model.billNo = res.result
			this.model.bpmBizTitle = this.userInfo?.employee?.name + '-' + this.model.billNo //标题
		},
		// 初始化新增需要的数据
		initForm() {
			this.model.proposer = this.userInfo.empId //当前登录人
			this.model.proposerDeptId = this.userInfo.deptId //当前登录人部门
			this.model.createTime = moment().format('YYYY-MM-DD HH:mm:ss')
		},
		async initInfo() {
			this.$set(this.model, 'applyMoney', 0)
			const response = await info({ id: this.id })
			const data = response?.result || {}
			this.model = Object.assign({}, data)

		},
		validateForm() {
			return this.$refs.form.validate()
		},
		async onSubmit() {
			await this.validateForm()
			this.isSubmit = true
			try {
				this.loading = true
				var url = ''
				if (this.id) {
					url = edit
				} else {
					url = add
				}
				const response = await url({
					...this.model,
				})
				if (response.success) {
					var that = this
					// 获取流程列表
					if (this.model.id) {
						this.getProcessList(this.model.id)
					} else {
						// 判断添加的时候是否有返回id值
						if (response.result) {
							this.getProcessList(response.result)
						}

					}
					this.$refs.uToast.show({
						message: response.message,
						type: 'default',
						complete() {
							// that.isSubmit = false
						}
					})
				}
			} finally {
				// this.loading = false
			}
		},
		async onDraft() {
			// await this.validateForm()
			this.isSubmit = true
			try {
				this.loading = true
				var url = ''
				if (this.id) {
					url = edit
				} else {
					url = add
				}
				const response = await url({
					...this.model,
					detailList: this.formData,
				})
				if (response.success) {
					this.$refs.uToast.show({
						message: response.message,
						type: 'default',
						complete() {
							this.isSubmit = false
							if (getCurrentPages().length > 1) {
								uni.navigateBack()
							} else {
								uni.reLaunch({
									url: '/pages/home/home'
								})
							}

						}
					})
				}
			} finally {
				this.loading = false
			}
		},
		// 获取流程列表
		getProcessList(id) {
			if (this.isSubmit) {
				this.$refs.processConfig.showPopup = true
				this.$refs.processConfig.formUrl = this.formUrl
				this.$refs.processConfig.formUrlMobile = this.formUrlMobile
				this.$refs.processConfig.flowCode = this.flowCode
				this.$refs.processConfig.edit(id, this.processId, this.beanName)
			}
		},
		// 提交流程成功
		onOk() {
			this.$refs.uToast.show({
				message: '提交成功',
				type: 'success',
				complete() {
					if (getCurrentPages().length > 1) {
						uni.navigateBack()
					} else {
						uni.reLaunch({
							url: '/pages/home/home'
						})
					}

				}
			})
		}
	},
}
</script>
  
<style scoped lang="scss">
@import url('@/assets/inputStyle.scss');

.trip-form-data {
	::v-deep {
		.u-form-item__body {
			padding-top: 2px;
			padding-bottom: 2px;
		}
	}
}

.checkbox-item {
	display: flex;
	align-items: center;
}
</style>
  